package com.java.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
         //判断是否是登录请求或跳转登录页面，如果是则放行
        if (uri.indexOf("/login") != -1 || uri.indexOf("/openLogin") != -1
                || uri.indexOf("/massage") != -1
                || uri.indexOf("/updatePwd") != -1
                || uri.indexOf("/media") != -1
                || uri.indexOf("/index") != -1
                || uri.indexOf("/CheckCode") != -1
                || uri.indexOf("/fogetMassage") != -1
                || uri.indexOf("/openForget") != -1
                || uri.indexOf("/forget") != -1
        ) {
            return true;
        }

        // 判断Session中是否存储了用户信息
        Object user = request.getSession().getAttribute("user");
        if (user != null) {
            return true;
        } else  {
            // 如果没有登录则跳转到登录页面
            response.sendRedirect("/openLogin");
        }

        return false;
    }

}
